// pages/dynamic/topics.js
let axios = require("../../utils/util.js")
const app = getApp()
const myaudio = wx.createInnerAudioContext() //音频
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isStart: null, //播放下标
    iscur: false, //播放状态
    user_list:{},
    voice_model: false,
    active: 1,
    item: {}, //话题消息
    type: 3, //1最热3最新
    page: 1, //页码
    recordList: [], //话题列表
    options: [{
        name: '微信',
        icon: 'wechat',
        openType: 'share'
      },
      {
        name: '朋友圈',
        icon: 'wechat-moments',
        description: "右上角分享到朋友圈"
      },
    ],
    showShare: false, //分享面板
    dy_userId: '', //当前选中人id
    recordId: '', //动态id
    user_list: {}, //我的信息
    reportshow: false, //举报动态popup
    page: 1,
    loading: false,
    collectRoom_data: true, //下拉默认还有数据
    imgList: [], //图片预览全集
    voide_src: '',
    voice_model: false,
    show: false,
  },
  start(data) {
    let that = this;
    let e = data.currentTarget.dataset.start[0]; //播放地址
    // console.log(e);
    let e2 = data.currentTarget.dataset.start[1]; //第几个下标
    that.setData({
      iscur: true
    })
    // console.log(that.data.isStart);
    // console.log(e2);
    if (that.data.isStart != e2) { //播放中
      console.log("播放");
      that.setData({
        isStart: e2
      })
      myaudio.src = e;
      myaudio.play(); //开始播放 
    } else { //暂停播放
      console.log("暂停");
      myaudio.pause()
    }
  },
  onChange(event) {
    this.setData({
      type: event.detail.name == 0 ? '1' : '3',
      page:1,
    })
    this.dync_tab()
  },
  showpopup(data) {
    let dy_userId = data.currentTarget.dataset.userid[0];
    let recordId = data.currentTarget.dataset.userid[1];
    this.setData({
      reportshow: true,
      dy_userId: dy_userId,
      recordId: recordId
    });
  },
  call(data) {
    let recordId = data.currentTarget.dataset.recordid;
    console.log(recordId);
    let params = {
      recordId: recordId
    };
    axios.axios({
      url: "/record/call",
      data: params
    }).then((res) => {
      wx.showToast({
        title: res.data.text,
        icon: 'none',
        duration: 3000
      })
    })
  },
  shar_model() {
    this.setData({
      showShare: true
    })
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  // 动态点赞
  likeRecord(data) {
    let recordId = data.currentTarget.dataset.recordid[0];
    let index = data.currentTarget.dataset.recordid[1];
    console.log(index);
    let params = {
      recordId: recordId
    };
    axios.axios({
      url: "/record/likeRecord",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        this.setData({
          ['recordList[' + index + '].isLiked']: res.data.result.type == 1 ? '1' : '0'
        })
        wx.showToast({
          title: res.data.text,
          icon: "none",
          duration: 2000
        })
      }
    })
  },
  onClose() {
    this.setData({
      reportshow: false
    });
  },
  onCloseShare() {
    this.setData({
      showShare: false
    })
  },
  toDetails(data) {
    wx.navigateTo({
      url: '/pages/dynamic/Details?recordId=' + data.currentTarget.dataset.recordid,
    })
  },
  // 跳转个人主页
  tomemberId(data) {
    wx.navigateTo({
      url: '/pages/me/me?memberId=' + data.currentTarget.dataset.perfectnumber,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  fhui_() {
    wx.navigateBack({ //返回
      delta: 1
    })
  },
  onLoad: function (options) {
    let that = this;
    var queryBean = JSON.parse(options.queryBean)
    this.setData({
      item: queryBean
    })
    this.dync_tab()
    myaudio.onPlay(() => { //开始播放
      console.log("开始的回调");
    })
    myaudio.onPause((res) => { //暂停播放
      console.log('暂停播放回调!');
      that.setData({
        isStart: null
      })
    })
    myaudio.onEnded((res) => { //结束播放
      console.log("结束的回调");
      that.setData({
        isStart: null,
        iscur: false
      })
      // myaudio.stop()
    })
  },
  dync_tab() {
    let params = {
      page: this.data.page,
      type: this.data.type,
      topicId: this.data.item.id,
      sex: 0,
    };
    axios.axios({
      url: "/record/recordList",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        res.data.result.recordList.forEach((item, index, arr) => {
          if (item.picUrl) {
            arr[index].picUrl = arr[index].picUrl.split(';');
          }
        })
        this.setData({
          recordList: res.data.result.recordList, //动态
        })

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let user_list = wx.getStorageSync('UserInformation')
    this.setData({
      user_list:user_list
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
